package com.itheima.bos.service.take_delivery.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.take_delivery.IPromotionRepository;
import com.itheima.bos.domain.page.PageBean;
import com.itheima.bos.domain.take_delivery.Promotion;
import com.itheima.bos.service.take_delivery.IPromotionService;

@Service
@Transactional
public class PromotionServiceImpl implements IPromotionService {

	// 注入dao层
	@Autowired
	private IPromotionRepository promotionRepository;

	// 保存数据
	@Override
	public void save(Promotion model) {
		promotionRepository.save(model);
	}

	// 分页查询
	@Override
	public Page<Promotion> pageQuery(Pageable pageable) {

		return promotionRepository.findAll(pageable);
	}

	// 前台分页查询
	@Override
	public PageBean<Promotion> findPageData(int page, int rows) {
		
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Promotion> pagedata = promotionRepository.findAll(pageable);
		
		// 封装查询对象到pageBean中
		PageBean<Promotion> pageBean = new PageBean<Promotion>();
		pageBean.setTotalCount(pagedata.getTotalElements());
		pageBean.setPageData(pagedata.getContent());
		return pageBean;
	}
	
	//根据id查询Promotion
	@Override
	public Promotion findById(Integer id) {
		return promotionRepository.findOne(id);
	}
	
	//修改状态码
	@Override
	public void updateStatus(Date date) {
		promotionRepository.updateStatus(date);
	}

}
